package org.maggioni.springsandbox.rehearsals.e05aop.e02xmlconfig.aspects;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.maggioni.springsandbox.rehearsals.e05aop.e02xmlconfig.model.BusinessObject;

public class LoggingAspect {


	public void loggingAdviceForEveryMethodInBusinessService(JoinPoint joinPoint) {
		System.out.println("(@Before) "+joinPoint.toString());
	}

	public void loggingAdviceWithPointcut() {
		System.out.println("(@Before) A logging advice with pointcut (allGetters) is called");
	}
	
	public void loggindAdviceForAllSetters(JoinPoint joinPoint, String parameter) {
		System.out.println("(@After) A setter is called with param " + joinPoint.getArgs()[0] + " (" + parameter + ")");
	}
	
	public void loggindAdviceForLoggableAnnotations() {
		System.out.println("(@After) A method with annotation 'Loggable' has been called");
	}
	
	public void loggingAdviceForStringOnly() {
		System.out.println("(@Before) A String getter is called.");
	}

	public void loggingAdviceForShapeServiceAfter() {
		System.out.println("(@After) A logging advice with pointcut (allGetters) is called");
	}
	
	public void loggingAdviceReturning() {
		System.out.println("(@AfterReturning) function doThrow completed without throwing");
	}

	public void loggingAdviceReturningAndModifyReturn(BusinessObject returnBusinessObject) {
		returnBusinessObject.setField1(returnBusinessObject.getField1()+" - modified");
		System.out.println("(@AfterReturning) doThrow has modified the output value!");
	}

	public void loggingAdviceThrowing(Exception ex) {
		System.out.println("(@AfterThrowing) doThrow ("+ex.getClass().getName()+")");
	}

//	public void loggingAdviceByArgs(String arg) {
//		System.out.println("(@Before) A logging advice by args (String) is called with parameter "+arg);
//	}

	public void loggingAdviceByArgs(JoinPoint joinPoint, int arg) {
		System.out.println("(@Before) A logging advice by args (int) is called with parameter "+arg+", join point: "+joinPoint.getSignature());
	}

	public Object loggingAdviceForAround(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
		BusinessObject ret = null;
		try {
			System.out.println("(@Around) Before advice");
			ret = (BusinessObject) proceedingJoinPoint.proceed();
			ret.setField1( ret.getField1()+" - modified by around advice" );
			System.out.println("(@Around) After returning");
		} catch (Throwable e) {
			System.out.println("(@Around) After throwing");
			throw e;
		}
		System.out.println("(@Around) After finally");
		return ret;
	}
	

}
